package cn.zmit.kuxi.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
import cn.zmit.kuxi.R;
import cn.zmit.kuxi.adapter.LuckyNumberAdapter;
import cn.zmit.kuxi.contants.Constants;
import cn.zmit.kuxi.contants.SPConstants;
import cn.zmit.kuxi.entity.LuckNumEntity;
import cn.zmit.kuxi.entity.LuckyNumEntity;
import cn.zmit.kuxi.event.EventFactory;
import cn.zmit.kuxi.request.RequestTask;

import com.xdroid.common.utils.PreferenceHelper;
import com.xdroid.request.extension.XDroidRequest;
import com.xdroid.request.extension.config.DataType;
import com.xdroid.request.extension.impl.OnRequestListenerAdapter;

public class PayFinishActivity extends BaseActivity {
	private Button btn_goon;
	private Button btn_to_join;
	private ListView lv_lucky_info;
	private List<LuckNumEntity> mLuckNumList = new ArrayList<LuckNumEntity>();
	private String user_id;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		initView();

		user_id = PreferenceHelper.readString(context,
				SPConstants.SHARED_PREFERENCE_NAME, SPConstants.USERID, "");
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("user_id", user_id);
		RequestTask.getInstance().doGetLuckyNum(this, map,
				new OnRequestListener());

		initListener();

		Constants.REFRESH_TAG = 1;
	}

	private void initView() {
		setTitleView("支付成功");
		setContentView(R.layout.activity_pay_finish);
		btn_goon = (Button) findViewById(R.id.btn_goon);
		btn_to_join = (Button) findViewById(R.id.btn_to_join);
		lv_lucky_info = (ListView) findViewById(R.id.lv_lucky_info);
	}

	private void initListener() {
		btn_goon.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				Log.e("info1===", "click");
				Intent intent = new Intent(PayFinishActivity.this,
						HomeActivity.class);
				intent.putExtra("jump_index", 1);
				startActivity(intent);
				// refresh shoppingcarts count
				int count = 0;
				EventFactory.sendUpdateCartCount(count);
			}
		});

		btn_to_join.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				Log.e("info1===", "click");
				Intent intent = new Intent(PayFinishActivity.this,
						JoinRecordActivity.class);
				startActivity(intent);
				// refresh shoppingcarts count
				int count = 0;
				EventFactory.sendUpdateCartCount(count);
			}
		});
	}

	// get lucky_num callback
	private class OnRequestListener extends OnRequestListenerAdapter<Object> {
		@Override
		public void onDone(XDroidRequest<?> request, String response,
				DataType dataType) {
			super.onDone(request, response, dataType);

			JSONObject root;
			try {
				root = new JSONObject(response);
				JSONArray array = root.getJSONArray("purchase_list");
				for (int i = 0; i < array.length(); i++) {
					JSONObject obj = array.getJSONObject(i);
					String sole_numbers = obj.getString("sole_numbers");
					String phase = obj.getString("phase");
					String name = obj.getString("name");
					LuckNumEntity entity = new LuckNumEntity(phase,
							sole_numbers, name);
					mLuckNumList.add(entity);
				}
			} catch (JSONException e) {
				e.printStackTrace();
			}
			LuckyNumberAdapter adapter = new LuckyNumberAdapter(mLuckNumList,
					PayFinishActivity.this);
			lv_lucky_info.setAdapter(adapter);
			Log.e("mLuckNumList", mLuckNumList + "");
		}
	}
}
